import Cache from '@/lib/core/Cache';

export default class ClipboardUtils {
    private static instance: ClipboardUtils;

    public static getInstance() {
        if (!this.instance) {
            this.instance = new ClipboardUtils();
        }
        return this.instance;
    }

    private mClipBoard: ApicloudModules['clipBoard'];

    constructor() {
        this.mClipBoard = api.require('clipBoard')
        if (!this.mClipBoard) {
            throw new Error('clipBoard 模块没引入')
        }
    }

    /**
     * 设置剪贴板
     */

    public set(val: string): Promise<boolean> {
        Cache.getFsCache().set('__last_copy_text__' as any, val);
        return new Promise((res) => {
            this.mClipBoard.set({
                value: val
            }, (ret) => {
                res(ret.status);
            })
        })
    }

    /**
     * 获取剪贴板数据
     */

    public get(): Promise<string> {
        return new Promise((res) => {
            this.mClipBoard.get((ret) => {
                res(ret.value);
            })
        })
    }
    /**
     * 获取最后一次设置的剪贴板内容
     */
    public getLastClip() {
        return Cache.getFsCache().get('__last_copy_text__' as any);
    }

}